﻿namespace ContactsSync.Infrastructure.Behaviors
{
    using System;
    using System.Windows;
    using System.Windows.Documents;
    using System.Windows.Media;
    using System.Windows.Media.Animation;

    public class InsertAdorner : Adorner
    {
        private readonly AdornerLayer adornerLayer;
        private readonly Pen pen;
        private readonly bool drawHorizontal;

        public InsertAdorner(bool isTopHalf, bool drawHorizontal, UIElement adornedElement, AdornerLayer adornerLayer)
            : base(adornedElement)
        {
            this.IsTopHalf = isTopHalf;
            this.adornerLayer = adornerLayer;
            this.drawHorizontal = drawHorizontal;
            this.adornerLayer.Add(this);
            this.pen = new Pen(new SolidColorBrush(Colors.White), 3.0);

            DoubleAnimation animation = new DoubleAnimation(0.5, 1, new Duration(TimeSpan.FromSeconds(0.5)))
                                            {
                                                AutoReverse = true,
                                                RepeatBehavior = RepeatBehavior.Forever
                                            };

            this.pen.Brush.BeginAnimation(Brush.OpacityProperty, animation);
        }

        public bool IsTopHalf { get; set; }

        public void Destroy()
        {
            this.adornerLayer.Remove(this);
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            Point startPoint;
            Point endPoint;

            if (!this.drawHorizontal)
            {
                this.DetermineHorizontalLinePoints(out startPoint, out endPoint);
            }
            else
            {
                this.DetermineVerticalLinePoints(out startPoint, out endPoint);
            }

            // Rect rect = new Rect(startPoint.X, startPoint.Y, this.AdornedElement.RenderSize.Width / 2, this.AdornedElement.RenderSize.Height);
            // drawingContext.DrawRectangle(new SolidColorBrush(Colors.Blue), this.pen, rect);
            drawingContext.DrawLine(this.pen, startPoint, endPoint);
        }

        private void DetermineHorizontalLinePoints(out Point startPoint, out Point endPoint)
        {
            double width = this.AdornedElement.RenderSize.Width;
            double height = this.AdornedElement.RenderSize.Height;

            if (!this.IsTopHalf)
            {
                startPoint = new Point(0, height);
                endPoint = new Point(width, height);
            }
            else
            {
                startPoint = new Point(0, 0);
                endPoint = new Point(width, 0);
            }
        }

        private void DetermineVerticalLinePoints(out Point startPoint, out Point endPoint)
        {
            double width = this.AdornedElement.RenderSize.Width;
            double height = this.AdornedElement.RenderSize.Height;

            if (!this.IsTopHalf)
            {
                startPoint = new Point(width, 0);
                endPoint = new Point(width, height);
            }
            else
            {
                startPoint = new Point(0, 0);
                endPoint = new Point(0, height);
            }
        }
    }
}